package mirrormonkey.rpc.module;

import mirrormonkey.rpc.messages.RpcResultMessage;

import com.jme3.network.Message;
import com.jme3.network.MessageConnection;
import com.jme3.network.MessageListener;

public class RpcResultListener implements MessageListener<MessageConnection> {

	public final CallRegistry callRegistry;

	public RpcResultListener(CallRegistry callRegistry) {
		this.callRegistry = callRegistry;
	}

	@Override
	public void messageReceived(MessageConnection source, Message message) {
		RpcResultMessage castMessage = (RpcResultMessage) message;
		RegisteredCall registeredCall = callRegistry
				.getRegisteredCall(castMessage.callId);
		// Can be null if call timed out
		if (registeredCall != null) {
			registeredCall.resultArrived(source, castMessage);
		}
	}
}
